package com.fang.unittest;

import java.io.*;

public class TestDriver {
    public static void main(String[] args) {
       test04();
    }

    /**
     * 这是通过创建新的类来管理文件的类型
     */
    private static void test01(){
        File f1 = new File("D:\\CloudMusic");
        File[] ff= f1.listFiles(new MyChoose());
        for (File file:ff
        ) {
            System.out.println(file);
        }
    }

    /**
     * 通过匿名内部类来管理文件的类型
     */
    private static void test02(){
        File f1 = new File("D:\\CloudMusic");
        File[] ff= f1.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                if(name.endsWith(".mp3")){
                    return true;
                }else{
                    return false;
                }
            }
        });
        for (File file:ff
        ) {
            System.out.println(file);
        }
    }

    /**
     * 使用io流进行文件的复制
     */
    private static void test03(){
        try(
               BufferedReader br = new BufferedReader(new InputStreamReader(
                       new FileInputStream("D:\\CloudMusic\\周黑猫 - 理想（Cover 赵雷）.mp3")));
               BufferedWriter bw = new BufferedWriter(
                       new OutputStreamWriter(new FileOutputStream("D:\\CloudMusic\\yyy（Cover 赵雷）.mp3")));
                ) {
                    char[] ch = new char[1024*5];
                    int len = br.read(ch);
                    while(len != -1){
                        bw.write(ch,0,len);
                        len = br.read(ch);
                    }

                    } catch (IOException ioException) {
            ioException.printStackTrace();
        }
    }

    /**
     * 使用序列化和反序列化对对象进行存储和复原
     */
    private static void test04(){
        try (
                ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:\\java\\lib\\student02.txt"));
                ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\java\\lib\\student02.txt"))
                ){
           Student stu = new Student("李四",20,"男");
           oos.writeObject(stu);
           Object ob = ois.readObject();
            System.out.println(ob);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

